package com.viking.util.database.query;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
@Slf4j

public class EnterInformationDataBase {
    private static ResultSet resultSet;
    private static int Max;

    public static int getMax() throws Exception {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username = "testing";
            String password = "Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url, username, password);
//        4.执行SQL对象Statement，执行SQL的对象

            Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = stmt.executeQuery("SELECT COUNT('*') FROM enterinformation");
            while (resultSet.next()) {
                Max = resultSet.getInt(1);
            }
            resultSet.close();
            connection.close();
        } catch (Exception e) {
            LocalDateTime dt = LocalDateTime.now();
            log.error(dt + "Failed to get EnterInformation quantity database");
        }

        return Max;

    }

    public static String[][] EnterInformationDataBase(int start, int limit) throws Exception {
        String[][] Data = new String[limit][10];

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
//        2.用户信息和url
            String url = "jdbc:mysql://8.130.32.3:3306/testing?useUnicode=true&characterEncoding=utf-8";
            String username = "testing";
            String password = "Yiee4M4sHAaNXAMf";
//        3.连接成功，数据库对象 Connection
            Connection connection = DriverManager.getConnection(url, username, password);
//        4.执行SQL对象Statement，执行SQL的对象

            Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = stmt.executeQuery("SELECT\n" +
                    "\tenterinformation.*\n" +
                    "FROM\n" +
                    "\tenterinformation\n" +
                    "LIMIT " + start + "," + limit);
            int i = 0;
            while (resultSet.next()) {
                Data[i][0] = resultSet.getString(1);
                Data[i][1] = resultSet.getString(2);
                Data[i][2] = resultSet.getString(3);
                Data[i][3] = resultSet.getString(4);
                Data[i][4] = resultSet.getString(5);
                Data[i][5] = resultSet.getString(6);
                Data[i][6] = resultSet.getString(7);
                Data[i][7] = resultSet.getString(8);
                Data[i][8] = resultSet.getString(9);
                Data[i][9] = resultSet.getString(10);
                i++;
            }
            for (i = 0; i < limit; i++) {
                System.out.print(Data[i][0] + "        ");
                System.out.print(Data[i][1] + "        ");
                System.out.print(Data[i][2] + "        ");
                System.out.print(Data[i][3] + "        ");
                System.out.print(Data[i][4] + "        ");
                System.out.print(Data[i][5] + "        ");
                System.out.print(Data[i][6] + "        ");
                System.out.print(Data[i][7] + "        ");
                System.out.print(Data[i][8] + "        ");
                System.out.print(Data[i][9] + "        ");
                System.out.println("");


            }
        }
        catch (Exception e)
        {
            LocalDateTime dt = LocalDateTime.now();
            log.error(dt+"Failed to get EnterInformation database");
        }
            return Data;
        }

    }

